home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / rendering / checkerboard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  2.7 KB  |  122 lines

  1. /*
  2.  * Copyright 1993, 1996, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /*  checkerboard.c  - open a window, clear the background, and 
  19.  *    render a checkerboard composed of flat shaded rectangles.
  20.  *
  21.  *    Escape key    - exit program
  22.  */
  23.  
  24. #include <GL/gl.h>
  25. #include <GL/glut.h>
  26.  
  27. #include <stdio.h>
  28.  
  29. /*  Function Prototypes  */
  30.  
  31. GLvoid  initgfx( GLvoid );
  32. GLvoid    keyboard( GLubyte, GLint, GLint );
  33. GLvoid  drawScene( GLvoid );
  34.  
  35. void printHelp( char * );
  36.  
  37. /* Global Definitions */
  38.  
  39. #define KEY_ESC    27    /* ascii value for the escape key */
  40.  
  41. void
  42. main( int argc, char *argv[] )
  43. {
  44.     GLsizei width, height;
  45.  
  46.     glutInit( &argc, argv );
  47.  
  48.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  49.     height = glutGet( GLUT_SCREEN_HEIGHT );
  50.     glutInitWindowPosition( width / 4, height / 4 );
  51.     glutInitWindowSize( width / 2, height / 2 );
  52.     glutInitDisplayMode( GLUT_RGBA );
  53.     glutCreateWindow( argv[0] );
  54.  
  55.     initgfx();
  56.  
  57.     glutKeyboardFunc( keyboard );
  58.     glutDisplayFunc( drawScene ); 
  59.  
  60.     printHelp( argv[0] );
  61.  
  62.     glutMainLoop();
  63. }
  64.  
  65. void
  66. printHelp( char *progname )
  67. {
  68.     fprintf(stdout, "\n%s - renders a checkerboard\n\n"
  69.         "Escape Key        - exit the program\n\n",
  70.         progname);
  71. }
  72.  
  73. GLvoid
  74. initgfx( GLvoid )
  75. {
  76.     glClearColor( 0.0, 0.0, 1.0, 1.0 );
  77.  
  78.     glOrtho( -2.0, 2.0, -2.0, 2.0, -1.0, 1.0 );
  79. }
  80.  
  81. GLvoid 
  82. keyboard( GLubyte key, GLint x, GLint y )
  83. {
  84.     switch (key) {
  85.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  86.         exit(0);
  87.     }
  88. }
  89.  
  90. GLvoid
  91. drawScene( GLvoid )
  92. {
  93.     GLfloat        x, y;
  94.  
  95.     static GLfloat    redColor[] = { 1.0, 0.0, 0.0 };
  96.     static GLfloat    blackColor[] = { 0.0, 0.0, 0.0 };
  97.  
  98.     /* alternate between red and black rectangles*/
  99.     GLboolean    useRed = GL_TRUE;  
  100.  
  101.     glClear( GL_COLOR_BUFFER_BIT );
  102.  
  103.     /* Draw a checkerboard composed of 0.1 by 0.1 rectangles 
  104.      * (For convenience, this code as been added to the class 
  105.      * library as Checkerboard()).
  106.      */
  107.  
  108.     for ( x = -1.0; x < 1.0; x += 0.1 ) {
  109.         useRed = !useRed;
  110.         for ( y = -1.0; y < 1.0; y += 0.1 ) {
  111.             if ( useRed )
  112.                 glColor3fv( redColor );
  113.             else
  114.                 glColor3fv( blackColor );
  115.             glRectf( x, y, x + 0.1, y + 0.1 );
  116.             useRed = !useRed;
  117.         }
  118.     }
  119.  
  120.     glFlush();
  121. }
  122.